
package com.ifcar.app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * DotIndicator中的每一项.
 * 
 * @author wjying
 */
public class DotIndicatorItem extends ImageView {
    private int mNum = -1;

    public DotIndicatorItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public DotIndicatorItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DotIndicatorItem(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mNum != -1) {
            final TextPaint tp = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
            final int imageWidth = getMeasuredWidth();
            final int imageHeight = getMeasuredHeight();
            final String str = String.valueOf(mNum + 1);
            final int textSize = (imageWidth - 3) * str.length();
            tp.setTextSize(textSize);
            tp.setColor(Color.BLACK);
            canvas.drawText(String.valueOf(mNum + 1), (imageWidth - textSize / 2) / 2,
                    (imageHeight + textSize) / 2, tp);
        }
    }

    /**
     * 显示数字.
     * 
     * @param num
     */
    public void setNum(int num) {
        mNum = num;
    }

    /**
     * 不显示数字.
     * 
     * @param num
     */
    public void resetNum() {
        mNum = -1;
    }

}
